import java.util.regex.Pattern;

public class Dish {
    private String name;
    private String DID;
    private double price;
    private int tot;

    Dish() {

    }

    Dish(String DID, String name, String price, String tot) {
        this.DID = DID;
        this.name = name;
        this.price = Double.parseDouble(price);
        this.tot = Integer.parseInt(tot);
    }

    // 创建新dish，如果数据错误返回null
    public static Dish newDish(String DID, String name, String price, String tot) {
        if (checkID(DID) && checkName(name) && checkPrice(price) && checkTot(tot)) {
            return new Dish(DID, name, price, tot);
        } else {
            return null;
        }
    }

    public void setDID(String dID) {
        DID = dID;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public void setTot(int tot) {
        this.tot = tot;
    }

    public String getDID() {
        return DID;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    public int getTot() {
        return tot;
    }

    public static boolean checkID(String id) {
        String regex = "[HCO]\\d{6}";
        Pattern pattern = Pattern.compile(regex);
        return pattern.matcher(id).matches();
    }

    public static boolean checkPrice(String price) {

        return MyUtil.checkNoNegativeDouble(price);

    }

    public static boolean checkTot(String tot) {

        return MyUtil.checkNoNegativeInt(tot);

    }

    public static boolean checkName(String name) {

        String regex = "[0-9a-zA-Z]+";
        Pattern pattern = Pattern.compile(regex);
        return pattern.matcher(name).matches();

    }

    public String toString() {
        return String.format("DID:%s,DISH:%s,PRICE:%.1f,TOTAL:%d", this.DID, this.name, this.price, this.tot);
    }
}
